package vn.com.demand.lux.preferences;

import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import com.swtdesigner.ResourceManager;

import vn.com.demand.lux.Activator;

public class DateFormatPreference extends PreferencePage implements
		IWorkbenchPreferencePage {

	private Combo comboDateType;
	/**
	 * Create the preference page
	 */
	public DateFormatPreference() {
		super();
		setTitle("Date Format");
		setImageDescriptor(ResourceManager.getPluginImageDescriptor(Activator.getDefault(), "icons/actions/date.png"));
		setDescription("Choose the format type for application");
	}

	/**
	 * Create contents of the preference page
	 * @param parent
	 */
	@Override
	public Control createContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		final Group dateFormatChooserGroup = new Group(container, SWT.NONE);
		dateFormatChooserGroup.setText("Date Format Chooser");
		dateFormatChooserGroup.setBounds(0, 23, 480, 123);

		final Label formatTypeLabel = new Label(dateFormatChooserGroup, SWT.NONE);
		formatTypeLabel.setAlignment(SWT.RIGHT);
		formatTypeLabel.setText("Date format type");
		formatTypeLabel.setBounds(37, 37, 84, 21);

		comboDateType = new Combo(dateFormatChooserGroup, SWT.NONE);
		comboDateType.setBounds(127, 34, 126, 21);
		String [] dateFormat = new String[] {"dd-MM-yyyy", "dd/MM/yyyy", "MM-dd-yyyy", "MM/dd/yyyy"};
		comboDateType.select(0);
		for (int i = 0; i < dateFormat.length; i++) {
			comboDateType.add(dateFormat[i]);
			if (dateFormat[i].equals(Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.DATE_FORMAT))) {
				comboDateType.select(i);
			}
		}
		return container;
	}

	@Override
	protected void performApply() {
		Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.DATE_FORMAT, comboDateType.getText());
		super.performApply();
	}

	@Override
	public boolean performOk() {
		Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.DATE_FORMAT, comboDateType.getText());
		return super.performOk();
	}

	@Override
	protected void performDefaults() {
		Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.DATE_FORMAT, PreferenceConstants.DEFAULT_DATE_FORMAT);
		super.performDefaults();
	}

	/**
	 * Initialize the preference page
	 */
	public void init(IWorkbench workbench) {
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}

}
